﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoodMan.Infrastructure.Entities.Entity;
using GoodMan.Infrastructure.Entities;
using GoodMan.Core.HitMonitor;

namespace GoodMan.Portal.Biz
{
    public class CollegeBiz
    {
        public static int iOrderByFlag_PubDate = 0; // 发布时间排序标志(1：升序 2：降序)
        public static int iOrderByFlag_Rate = 0; // 点击率排序标志(1：升序 2：降序)

        public static bool SavePublishInfo(Information pubinfo)
        {
            return pubinfo.PostAdd() != null;
        }

        public static ItemListBox<Information> GetAllPubInfo(int oper, int page, int takeSize)
        {
            var count = new Information().Count(c => c.Id > 0);
            using (Context ctx = new Context())
            {
                var data = new List<Information>();
                if (oper == 1) // 按发布时间排序
                {
                    if (iOrderByFlag_PubDate == 1) // 需降序
                    {
                        data = ctx.Information
                           .OrderByDescending(c => c.PubDate)
                           .Skip(page * takeSize)
                           .Take(takeSize)
                           .ToList()
                           .Select(c => new Information().Set(c))
                           .ToList();
                        iOrderByFlag_PubDate = 2;
                    }
                    else if (iOrderByFlag_PubDate == 2) // 需升序
                    {
                        data = ctx.Information
                           .OrderBy(c => c.PubDate)
                           .Skip(page * takeSize)
                           .Take(takeSize)
                           .ToList()
                           .Select(c => new Information().Set(c))
                           .ToList();
                        iOrderByFlag_PubDate = 1;
                    }
                    iOrderByFlag_Rate = 0;
                }
                else if (oper == 2) // 按点击率排序
                {
                    if (iOrderByFlag_Rate == 1) // 需降序
                    {
                        data = ctx.Information
                           .OrderByDescending(c => c.CTR)
                           .Skip(page * takeSize)
                           .Take(takeSize)
                           .ToList()
                           .Select(c => new Information().Set(c))
                           .ToList();
                        iOrderByFlag_Rate = 2;
                    }
                    else if (iOrderByFlag_Rate == 2) // 需升序
                    {
                        data = ctx.Information
                           .OrderBy(c => c.CTR)
                           .Skip(page * takeSize)
                           .Take(takeSize)
                           .ToList()
                           .Select(c => new Information().Set(c))
                           .ToList();
                        iOrderByFlag_Rate = 1;
                    }
                    iOrderByFlag_PubDate = 0;
                }
                else
                {
                    data = ctx.Information
                           .OrderByDescending(c => c.Id)
                           .Skip(page * takeSize)
                           .Take(takeSize)
                           .ToList()
                           .Select(c => new Information().Set(c))
                           .ToList();
                    iOrderByFlag_PubDate = 0;
                    iOrderByFlag_Rate = 0;
                }

                var list = new ItemListBox<Information>(data).BuildPage(
                   count, page, takeSize, new PageParameter() { Style = "home" });
                return list;
            }
        }

        public static bool ModifyPubInfoById(Information pubinfo)
        {
            return pubinfo.PostModify() != null;
        }

        public static bool DeletePubInfoByID(int pubID)
        {
            try
            {
                var pubinfo = new Information().Find(pubID.ToString());
                if (pubinfo != null)
                {
                    return pubinfo.PostDelete() != null;
                }
            }
            catch (Exception ex)
            {
                //日志记录
                return false;
            }
            return false;
        }
    }
}
